package com.example.securitydemo.service.impl;

import com.aaa.entity.TabRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.securitydemo.mapper.TabRoleMapper;
import com.example.securitydemo.mapper.TabRoleMenuMapper;
import com.example.securitydemo.service.ITabRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 于永利
 * @since 2023-07-14
 */
@Service
public class TabRoleServiceImpl extends ServiceImpl<TabRoleMapper, TabRole> implements ITabRoleService {

    //
    @Resource
    private TabRoleMenuMapper roleMenuMapper;
    @Resource
    private TabRoleMapper tabRoleMapper;
    @Override
    @Transactional
    public void addRoleAndRoleMenu(TabRole roleData, String menuids) {
        // 添加角色的信息
        tabRoleMapper.insert(roleData);
        int rid=roleData.getRid();
        // 添加角色的资源信息
        roleMenuMapper.addRoleMenu(rid, Arrays.asList(menuids.split(",")));



    }

    @Override
    @Transactional
    public void updRoleAndRoleMenu(TabRole roleData, String menuids) {
        tabRoleMapper.updateById(roleData);
        int rid=roleData.getRid();
        // 先根据rid 删除数据  然后添加新的数据
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("rid",rid);
        roleMenuMapper.delete(queryWrapper);

        // 添加新的数据
        roleMenuMapper.addRoleMenu(rid, Arrays.asList(menuids.split(",")));

    }
}
